import { v4 as uuidv4 } from 'uuid';
import {
  ITEM_TYPE,
  type CultivationSkillItemConfig,
  type EquipmentItemConfig,
  type MaterialItemConfig,
} from '../../data/item/type';
import type { InventorySlotItem } from '../../model/inventory/type';
import { ItemCalculator } from '../item/calculator';
import type { ItemConfig } from '../../data/item';

export class InventoryFactory {
  static createItem(config: ItemConfig, quantity: number): InventorySlotItem[] {
    switch (config.type) {
      case ITEM_TYPE.CULTIVATION_SKILL:
        return [InventoryFactory.createCultivationSkillItem(config, quantity)];
      case ITEM_TYPE.EQUIPMENT:
        return Array.from({ length: quantity }, () =>
          InventoryFactory.createEquipItem(config)
        );
      case ITEM_TYPE.MATERIAL:
        return [InventoryFactory.createMaterialItem(config, quantity)];
      default:
        throw new Error('不支持的物品类型');
    }
  }

  /** 创建可放入背包的装备 */
  static createEquipItem(config: EquipmentItemConfig): InventorySlotItem {
    const rarity = ItemCalculator.rollDropEquipmentRarity();
    const baseAttrs = ItemCalculator.rollEquipmentBaseAttributes(
      config,
      rarity
    );

    return {
      id: uuidv4(),
      itemId: config.id,
      quantity: 1,
      payload: {
        equip: {
          rarity,
          baseAttrs,
          enhance: { level: 0, failProtection: 0 },
        },
      },
    };
  }

  /** 创建可放入背包的功法 */
  static createCultivationSkillItem(
    config: CultivationSkillItemConfig,
    quantity: number
  ): InventorySlotItem {
    return {
      id: uuidv4(),
      itemId: config.id,
      quantity,
    };
  }

  /** 创建可放入背包的材料 */
  static createMaterialItem(
    config: MaterialItemConfig,
    quantity: number
  ): InventorySlotItem {
    return {
      id: uuidv4(),
      itemId: config.id,
      quantity,
    };
  }
}
